﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using DiscoveryLogic.Analytics.DataObjects.Networks;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.Analytics.DataObjects
{
    public class Arc : Edge
    {
        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public Arc()
            : base()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public Arc(Guid id, string label, double len, Guid fromVID, Guid toVID)
            : base(id, label, len, fromVID, toVID)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        public Arc(Guid id, string label, double len,
            Guid fromVID, Guid toVID, Link tag)
            : this(id, label, len, fromVID, toVID)
        {
            this.Tag = tag;
        }
        #endregion

        #region xml
        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="arcNode"></param>
        public new void Save(ref XmlDocument xDoc, XmlNode arcNode)
        {
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                object propValue = prop.GetValue(this, null);
                if (propValue != null)
                {
                    XmlDataUtil.UpdateAttribute(ref xDoc, arcNode, prop.Name, propValue.ToString());
                }
            }
            if (this.Tag != null)
            {
                XmlNode linkNode = XmlDataUtil.AddElement(ref xDoc, arcNode, "Link");
                this.Tag.Save(ref xDoc, linkNode);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="arcNode"></param>
        /// <returns></returns>
        public new Arc Read(XmlNode arcNode)
        {
            Arc e = (Arc)Activator.CreateInstance(this.GetType());
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                if (arcNode.Attributes.GetNamedItem(prop.Name) != null)
                {
                    string propValue = XmlDataUtil.GetAttributeValue(arcNode, prop.Name, "");
                    SimpleDataType dataType = SimpleDataTypeUtil.ToSimpleDataType(prop.PropertyType);
                    object propValue2 = SimpleDataTypeUtil.ToDbValue(dataType, propValue.ToString());
                    if (propValue2 != null && propValue2 != DBNull.Value)
                        prop.SetValue(e, propValue2, null);
                }
            }
            e.Tag = new Link();
            if (arcNode.ChildNodes != null && arcNode.ChildNodes.Count == 1)
            {
                XmlNode linkNode = arcNode.ChildNodes[0];
                e.Tag = Link.Read(linkNode);
            }

            return e;
        }

        ///<summary>
        ///</summary>
        ///<returns></returns>
        public new Arc Clone()
        {
            Arc e = new Arc();
            PropertyInfo[] props = this.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                object propVal = prop.GetValue(this, null);
                prop.SetValue(e, propVal, null);
            }
            return e;
        }
        #endregion
    }
}
